package router

import (
    v1 "ginBlog/api/v1"
    "ginBlog/middleware"
    "ginBlog/utils"
    "github.com/gin-gonic/gin"
    "net/http"
)

func InitRouter() *gin.Engine {
    r := gin.New()
    r.HandleMethodNotAllowed = true
    r.NoMethod(func(c *gin.Context) {
        c.JSON(http.StatusMethodNotAllowed, gin.H{"result": false, "error": "Method Not Allowed"})
        return
    })

    // 使用自定义日志 ，允许跨域请求
    r.Use(middleware.CustomLogger(), middleware.CorsMiddleware())
    r.Use(gin.Recovery())

    apiV1 := r.Group(utils.AppInfo.ApiBaseUrl)
    {
        apiV1.Static(utils.AppInfo.StaticBasePath, utils.AppInfo.UploadBasePath)
        apiV1.POST("/user/login", v1.Login)
        apiV1.POST("/user/logout", v1.Logout)

        // GET 方法没有使用 JWT 认证
        apiV1.GET("/user/info", v1.GetUserInfo)
        apiV1.GET("/user/about", v1.GetUserAbout)

        apiV1.GET("/tags", v1.GetAllTags)
        apiV1.POST("/tags", v1.CreateTag)

        apiV1.GET("/articles", v1.GetArticles)

        apiV1.GET("/category", v1.GetAllCategory)
        apiV1.POST("/category", v1.CreateCategory)

        //apiV1.Use(middleware.JWt())

        apiV1.PATCH("/user/edit", v1.EditUser)


        apiV1.PUT("/tags/:id", v1.EditTag)
        apiV1.DELETE("/tags/:id", v1.DeleteTag)

        //apiV1.GET("/category", v1.GetAllCategory)
        //apiV1.POST("/category", v1.CreateCategory)
        apiV1.PUT("/category/:id", v1.EditCategory)
        apiV1.DELETE("/category/:id", v1.DeleteCategory)

        //apiV1.GET("/articles", v1.GetArticles)
        apiV1.GET("/articles/:id", v1.GetArticle)
        apiV1.POST("/articles", v1.CreateArticle)
        apiV1.DELETE("/articles/:id", v1.DeleteArticle)
        apiV1.PUT("/articles/:id", v1.EditArticle)


    }
    return r
}
